<template>
	<div class="container groupbuy_set">
		<el-form v-loading="formLoading" :model="form" ref="ruleForm">
			<h3>拼团参数</h3>
			<el-form-item class="margin_t_20">
				<div class="dflex_vertical_c">
					<div class="front_txt margin_r_10">拼团失败</div>
					<el-radio-group :disabled="addBtnLoading" v-model="group_failed">
						<el-radio :label="1">自动退款</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt margin_r_10">拼团攻略</div>
					<use-editor :disabled="addBtnLoading" ref="editor" @editorChange="editorChange"></use-editor>
				</div>
			</el-form-item>
			<el-form-item label-width="120px">
				<el-button round icon="el-icon-check" :loading="addBtnLoading" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmzmall-market-groupbuy-param';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					notice: ''
				},
				group_failed: 1,
				addBtnLoading: false
			};
		},
		methods: {
			async loadData() {
				this.formLoading = true;
				await this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						res.datas = res.datas || {};

						this.dataId = res.datas._id || '';

						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}

						if (res.datas && res.datas.notice) {
							this.form.notice = res.datas.notice;
							this.$refs.editor.setContent(this.form.notice);
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.addBtnLoading = true;
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			editorChange(res) {
				this.form.notice = res;
			}
		},
		created() {
			this.loadData();
		}
	};
</script>

<style>
	>>>.input {
		width: 70px;
		margin: 0 10px;
	}

	>>>.input .el-input__inner {
		width: 100%;
		background-color: #f6f6f6;
		border-color: #f6f6f6;
		padding: 0 0 0 15px;
	}

	.front_txt {
		width: 110px;
		text-align: right;
		font-size: 13px;
	}

	.rear_txt {
		font-size: 13px;
		display: flex;
		align-items: center;
	}
</style>
